package com.fowo.api.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.WarehouseBin;
import com.fowo.api.entity.WarehouseBinProducts;
import com.fowo.api.mapper.WarehouseBinMapper;
import com.fowo.api.mapper.WarehouseBinProductsMapper;
import com.fowo.api.mapper.WarehouseBinRulesMapper;
import com.fowo.api.model.warehousebinrules.WarehouseBinRulesItemVo;
import com.fowo.api.model.warehousebinrules.WarehouseBinRulesSearchParamPo;
import com.fowo.api.service.WarehouseBinProductsService;
import com.fowo.api.service.WarehouseBinRulesService;
import com.fowo.api.service.WarehouseBinService;
import liquibase.pro.packaged.A;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@DS("ds24")
public class WarehouseBinRulesServiceImpl implements WarehouseBinRulesService {
    @Resource
    private WarehouseBinRulesMapper warehouseBinRulesMapper;
    @Resource
    private WarehouseBinProductsMapper warehouseBinProductsMapper;
    @Resource
    private WarehouseBinMapper warehouseBinMapper;
    @Resource
    private WarehouseBinProductsService warehouseBinProductsService;
    @Resource
    private WarehouseBinService warehouseBinService;

    @Override
    public WarehouseBinRulesItemVo search(WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo) throws Exception {
        LambdaQueryWrapper<WarehouseBinProducts> wrapper = Wrappers.lambdaQuery(WarehouseBinProducts.class).eq(WarehouseBinProducts::getProductId, warehouseBinRulesSearchParamPo.getProductId());
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getShopId())) {
            wrapper.isNull(WarehouseBinProducts::getShopId);
        } else {
            wrapper.eq(WarehouseBinProducts::getShopId, warehouseBinRulesSearchParamPo.getShopId());
        }
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getFnSku())) {
            wrapper.isNull(WarehouseBinProducts::getFnskuName);
        } else {
            wrapper.eq(WarehouseBinProducts::getFnskuName, warehouseBinRulesSearchParamPo.getFnSku());
        }
        List<WarehouseBinProducts> warehouseBinProductsList = warehouseBinProductsMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(warehouseBinProductsList)) {
            if (warehouseBinRulesSearchParamPo.getType() == 1) {
                //可能是虚拟 没有找主表
//                WarehouseBin warehouseBin = warehouseBinMapper
//                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
//                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
//                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
//                //可用得
//                if (warehouseBin == null) {
//                    throw new Exception("没有查到对应仓位");
//                }
//                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
//                warehouseBinRulesItemVo.setId(warehouseBin.getId());
//                warehouseBinRulesItemVo.setName("可用暂存");
                WarehouseBin warehouseBinRulesItemVo = warehouseBinMapper.selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                        .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                        .eq(WarehouseBin::getStorageBin, "可用暂存"));
                if(warehouseBinRulesItemVo==null){
                    throw new Exception("没有查到对应仓位");
                }else{
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo2 = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo2.setId(warehouseBinRulesItemVo.getId());
                    warehouseBinRulesItemVo2.setName("可用暂存");
                    return warehouseBinRulesItemVo2;
                }
//                return warehouseBinRulesItemVo;
            } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
                //可能是虚拟 没有找主表
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
                //次品
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName("次品暂存");
                return warehouseBinRulesItemVo;
            }

        }
        //可用得
        if (warehouseBinRulesSearchParamPo.getType() == 1) {
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
//                throw new Exception("没有查到对应仓位");
                //查可用暂存
//                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
//                warehouseBinRulesItemVo.setId(warehouseBin.getId());
//                warehouseBinRulesItemVo.setName("可用暂存");
                WarehouseBin warehouseBinRulesItemVo = warehouseBinMapper.selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                        .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                        .eq(WarehouseBin::getStorageBin, "可用暂存"));
                if(warehouseBinRulesItemVo==null){
                    throw new Exception("没有查到对应仓位");
                }else{
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo2 = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo2.setId(warehouseBinRulesItemVo.getId());
                    warehouseBinRulesItemVo2.setName("可用暂存");
                    return warehouseBinRulesItemVo2;
                }
            }else{
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName(warehouseBin.getStorageBin());
                return warehouseBinRulesItemVo;
            }
        } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
            //次品
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                throw new Exception("没有查到对应仓位");
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("次品暂存");
            return warehouseBinRulesItemVo;
        } else if (warehouseBinRulesSearchParamPo.getType() == 3) {
            //可用
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                throw new Exception("没有查到对应仓位");
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("可用");
            return warehouseBinRulesItemVo;
        }

        return null;
    }

    @Override
    public List<WarehouseBinRulesItemVo> searchRules(WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo) throws Exception {
        List<WarehouseBinRulesItemVo> list = new ArrayList<>();
        LambdaQueryWrapper<WarehouseBinProducts> wrapper = Wrappers.lambdaQuery(WarehouseBinProducts.class).eq(WarehouseBinProducts::getProductId, warehouseBinRulesSearchParamPo.getProductId());
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getShopId())) {
            wrapper.isNull(WarehouseBinProducts::getShopId);
        } else {
            wrapper.eq(WarehouseBinProducts::getShopId, warehouseBinRulesSearchParamPo.getShopId());
        }
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getFnSku())) {
            wrapper.isNull(WarehouseBinProducts::getFnsku);
        } else {
            wrapper.eq(WarehouseBinProducts::getShopId, warehouseBinRulesSearchParamPo.getFnSku());
        }
        List<WarehouseBinProducts> warehouseBinProductsList = warehouseBinProductsMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(warehouseBinProductsList)) {
            if (warehouseBinRulesSearchParamPo.getType() == 1) {
                //可能是虚拟 没有找主表
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "2").last(" limit 1")); //可用暂存
                //可用暂存
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("可用暂存");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);

                    list.add(warehouseBinRulesItemVo);
                }
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "5").last(" limit 1")); //可用
                //可用
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("可用");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
            } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
                //次品暂存
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "3").last(" limit 1"));
                //次品暂存
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("次品暂存");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
                //次品
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "6").last(" limit 1"));
                //次品
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    warehouseBinRulesItemVo.setName("次品");
                    list.add(warehouseBinRulesItemVo);
                }

            }


        } else {
            if (warehouseBinRulesSearchParamPo.getType() == 1) {
                //可用暂存
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                                .eq(WarehouseBin::getType, "2").last(" limit 1"));
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("可用暂存");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
                //可用
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                                .eq(WarehouseBin::getType, "5").last(" limit 1"));
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("可用");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
            } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
                //次品暂存
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                                .eq(WarehouseBin::getType, "3").last(" limit 1"));
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("次品暂存");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
                //次品
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                                .eq(WarehouseBin::getType, "6").last(" limit 1"));
                if (warehouseBin != null) {
                    WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                    warehouseBinRulesItemVo.setId(warehouseBin.getId());
                    warehouseBinRulesItemVo.setName("次品");
                    Integer qty = warehouseBinRulesMapper.getInventoryPlacementCount(warehouseBinRulesSearchParamPo, warehouseBin.getId());
                    getWarehouseBinRulesSearchParamPo(warehouseBinRulesItemVo, warehouseBinRulesSearchParamPo, qty);
                    list.add(warehouseBinRulesItemVo);
                }
            }


        }
        System.out.println(list.toString());
        return list;
    }

    /**
     * type 1  先查可用 在查暂存 如果可用没查到 在查再存
     * 2  次品同上
     *
     * @param warehouseBinRulesSearchParamPo
     * @return
     * @throws Exception
     */
    @Override
    public WarehouseBinRulesItemVo searchNew(WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo) throws Exception {
        // 仓位产品根据维度查找
        // 仓位    1.仓位产品存在:    根据绑定关系查找  绑定关系拼接.in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
        //         2.仓位产品不存在: 根据仓库+类型查可公共仓位  仓位类型包括:1:待检暂存(预留) 2:可用暂存 3:次品暂存 4:拣货暂存(预留) 5:可用 6:次品 7:上架暂存(预留) 8:可用在途暂存(预留) 9:次品在途暂存(预留)
        LambdaQueryWrapper<WarehouseBinProducts> wrapper = Wrappers.lambdaQuery(WarehouseBinProducts.class).eq(WarehouseBinProducts::getProductId, warehouseBinRulesSearchParamPo.getProductId());
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getShopId())) {
            wrapper.isNull(WarehouseBinProducts::getShopId);
        } else {
            wrapper.eq(WarehouseBinProducts::getShopId, warehouseBinRulesSearchParamPo.getShopId());
        }
        if (ObjectUtils.isEmpty(warehouseBinRulesSearchParamPo.getFnSku())) {
            wrapper.isNull(WarehouseBinProducts::getFnskuName);
        } else {
            wrapper.eq(WarehouseBinProducts::getFnskuName, warehouseBinRulesSearchParamPo.getFnSku());
        }
        List<WarehouseBinProducts> warehouseBinProductsList = warehouseBinProductsMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(warehouseBinProductsList)) {
            if (warehouseBinRulesSearchParamPo.getType() == 1) {
                //可能是虚拟 没有找主表 直接查询暂存 没有直接提示异常
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "2")
                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
                //可用得
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName("可用暂存");
                return warehouseBinRulesItemVo;
            } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
                //可能是虚拟 没有找主表
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getType, "3")
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
                //次品
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName("次品暂存");
                return warehouseBinRulesItemVo;
            } else if (warehouseBinRulesSearchParamPo.getType() == 3) {
                //可能是虚拟 没有找主表
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getType, "5")
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
                //次品
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName("可用");
                return warehouseBinRulesItemVo;
            } else if (warehouseBinRulesSearchParamPo.getType() == 4) {
                //可能是虚拟 没有找主表
                WarehouseBin warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getType, "6")
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getSysType, "0").last(" limit 1"));
                //次品
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
                WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
                warehouseBinRulesItemVo.setId(warehouseBin.getId());
                warehouseBinRulesItemVo.setName("次品");
                return warehouseBinRulesItemVo;
            }

        }
        //可用的 查不到在查暂存  type : 1  可用暂存  2 次品暂存 3 可用 4 次品
        if (warehouseBinRulesSearchParamPo.getType() == 1) {
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .eq(WarehouseBin::getType, "5")
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "2").last(" limit 1"));
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("可用暂存");
            return warehouseBinRulesItemVo;
        } else if (warehouseBinRulesSearchParamPo.getType() == 2) {
            //次品 查不到在查暂存
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getType, "6")
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "3").last(" limit 1"));
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("次品暂存");
            return warehouseBinRulesItemVo;
        } else if (warehouseBinRulesSearchParamPo.getType() == 3) {
            //可用
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .eq(WarehouseBin::getType, "5")
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "5")
                                .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("可用");
            return warehouseBinRulesItemVo;
        } else if (warehouseBinRulesSearchParamPo.getType() == 4) {
            //次品
            WarehouseBin warehouseBin = warehouseBinMapper
                    .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getType, "6")
                            .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                            .in(WarehouseBin::getId, warehouseBinProductsList.stream().map(WarehouseBinProducts::getParentId).collect(Collectors.toList()))
                            .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
            if (warehouseBin == null) {
                warehouseBin = warehouseBinMapper
                        .selectOne(Wrappers.lambdaQuery(WarehouseBin.class)
                                .eq(WarehouseBin::getWarehouseId, warehouseBinRulesSearchParamPo.getWarehouseId())
                                .eq(WarehouseBin::getType, "6")
                                .eq(WarehouseBin::getSysType, "1").last(" limit 1"));
                if (warehouseBin == null) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            WarehouseBinRulesItemVo warehouseBinRulesItemVo = new WarehouseBinRulesItemVo();
            warehouseBinRulesItemVo.setId(warehouseBin.getId());
            warehouseBinRulesItemVo.setName("次品");
            return warehouseBinRulesItemVo;
        }

        return null;
    }

    @Override
    public List<WarehouseBinProducts> searchWarehouseBinProductsList(List<Long> ids) throws Exception {
        LambdaQueryWrapper<WarehouseBinProducts> wrapper = Wrappers.lambdaQuery(WarehouseBinProducts.class).in(WarehouseBinProducts::getProductId, ids);
        return warehouseBinProductsMapper.selectList(wrapper);
    }


    @Override
    public List<WarehouseBin> searchWarehouseBinByWarehouseIdList(List<Long> ids, int type) throws Exception {
        if (type == 1) {
            List<WarehouseBin> warehouseBin = warehouseBinMapper
                    .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                            .in(WarehouseBin::getWarehouseId, ids)
                            .eq(WarehouseBin::getType, "2"));
            if (CollectionUtils.isEmpty(warehouseBin)) {
                throw new Exception("没有查到对应仓位");
            }
            return warehouseBin;

        } else if (type == 2) {
            List<WarehouseBin> warehouseBin = warehouseBinMapper
                    .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                            .in(WarehouseBin::getWarehouseId, ids)
                            .eq(WarehouseBin::getType, "3"));
            if (CollectionUtils.isEmpty(warehouseBin)) {
                throw new Exception("没有查到对应仓位");
            }
            return warehouseBin;
        } else {
            throw new Exception("指定类型错误");
        }
    }

    @Override
    public List<WarehouseBin> searchWarehouseBinList(List<Long> ids, List<Long> warehouseIdS, int type) throws Exception {
        if (type == 1) {
            List<WarehouseBin> warehouseBin = warehouseBinMapper
                    .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getType, "5")
                            .in(WarehouseBin::getId, ids)
                            .eq(WarehouseBin::getSysType, "1"));
            if (CollectionUtils.isEmpty(warehouseBin)) {
                warehouseBin = warehouseBinMapper
                        .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                                .in(WarehouseBin::getWarehouseId, warehouseIdS)
                                .eq(WarehouseBin::getType, "2"));
                if (CollectionUtils.isEmpty(warehouseBin)) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            return warehouseBin;
        } else if (type == 2) {
            List<WarehouseBin> warehouseBin = warehouseBinMapper
                    .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                            .eq(WarehouseBin::getType, "6")
                            .in(WarehouseBin::getId, ids)
                            .eq(WarehouseBin::getSysType, "1"));
            if (CollectionUtils.isEmpty(warehouseBin)) {
                warehouseBin = warehouseBinMapper
                        .selectList(Wrappers.lambdaQuery(WarehouseBin.class)
                                .in(WarehouseBin::getWarehouseId, warehouseIdS)
                                .eq(WarehouseBin::getType, "3"));
                if (CollectionUtils.isEmpty(warehouseBin)) {
                    throw new Exception("没有查到对应仓位");
                }
            }
            return warehouseBin;
        } else {
            throw new Exception("指定类型错误");
        }
    }

    /**
     * 推荐数
     *
     * @param warehouseBinRulesSearchParamPo
     * @param qty
     */
    private void getWarehouseBinRulesSearchParamPo(WarehouseBinRulesItemVo warehouseBinRulesItemVo, WarehouseBinRulesSearchParamPo warehouseBinRulesSearchParamPo, Integer qty) {
        if (warehouseBinRulesSearchParamPo.getQty() != null || warehouseBinRulesSearchParamPo.getQty() != 0) {
            if (warehouseBinRulesSearchParamPo.getQty() > qty) {
                warehouseBinRulesItemVo.setQty(qty);
                warehouseBinRulesSearchParamPo.setQty(warehouseBinRulesSearchParamPo.getQty() - qty);
            } else {
                warehouseBinRulesSearchParamPo.setQty(0);
                warehouseBinRulesItemVo.setQty(warehouseBinRulesSearchParamPo.getQty());
            }
        } else {
            warehouseBinRulesItemVo.setQty(qty);
        }
    }
}
